package top.wineye.spring.dependency.inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.wineye.spring.dependency.inject.annotation.GroupedBiz;
import top.wineye.spring.dependency.pojo.Biz;

import java.util.Collection;

@Configuration
public class QualifierInject {

    @Autowired
    @Qualifier("biz1")
    Biz biz1;

    @Autowired
    @Qualifier
    Collection<Biz> qualifiedBiz;

    @Autowired
    @GroupedBiz
    Collection<Biz> groupedBiz;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext(QualifierInject.class);

        QualifierInject bean = ctx.getBean(QualifierInject.class);

        System.out.println(bean.biz1);
        System.out.println(bean.qualifiedBiz);
        System.out.println(bean.groupedBiz);

        ctx.close();
    }

    private static Biz creatBiz(String s) {
        Biz biz = new Biz();
        biz.setBiz(s);
        return biz;
    }

    @Bean
    public Biz biz1() {
        return creatBiz("1");
    }

    @Bean
    public Biz biz2() {
        return creatBiz("2");
    }


    @Bean
    @Qualifier
    public Biz biz3() {
        return creatBiz("3");
    }

    @Bean
    @Qualifier
    public Biz biz4() {
        return creatBiz("4");
    }

    @Bean
    @GroupedBiz
    public Biz biz5() {
        return creatBiz("5");
    }

    @Bean
    @GroupedBiz
    public Biz biz6() {
        return creatBiz("6");
    }
}
